package com.yc.navy.dayfresh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.dayfresh.bean.MemberInfo;
import com.yc.navy.dayfresh.dto.OrderDetailInfoDTO;
import com.yc.navy.dayfresh.enums.ResultEnum;
import com.yc.navy.dayfresh.service.IOrderInfoService;
import com.yc.navy.dayfresh.util.ConstantInfo;
import com.yc.navy.dayfresh.vo.ResultVO;

@RestController
@RequestMapping("/order")
public class OrderInfoController {
	@Autowired
	private IOrderInfoService orderInfoService;
	
	private Long getLoginUser(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfo.CURRENTLOGINMEMBERINFO);
		if(obj == null) {
			return null;
		}
		MemberInfo mf = (MemberInfo) obj;
		return mf.getMno();
	}
	
	@PostMapping("/add")
	public ResultVO add(@RequestParam("pnos[]") String[] pnos, String ano ,HttpSession session) {
		Long mno = getLoginUser(session);
		if(mno == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		
		try {
			Map<String, Object> map = orderInfoService.add(pnos, ano, mno);
			if(map != null && !map.isEmpty()) {
				return new ResultVO(ResultEnum.SUCCESS,map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@GetMapping("/finds")
	public ResultVO finds(HttpSession session) {
		Long mno = getLoginUser(session);
		if(mno == null) {
			return new ResultVO (ResultEnum.LOGIN_ERROR);
		}
		
		try {
			Map<String,Object> map = new HashMap<String,Object> ();
			map.put("mno", mno);
			List<OrderDetailInfoDTO> list = orderInfoService.finds(map);
			System.out.println("finds*****"+list);
			if(list != null && !list.isEmpty()) {
				return new ResultVO(ResultEnum.SUCCESS,list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.DATA_NULL);
	}
}
